require("../../19EB63A409CE66DF7F8D0BA3775A8AF7.js");

var t = getApp(), a = null;

Page({
    data: {
        pageIsLoad: !1,
        is_check: -1,
        is_ios_pay: 0,
        system: "",
        balance: 0,
        vip_expire_time: "",
        tasks: null,
        goodsList: [],
        goods_id: 0,
        vipList: [],
        vip_id: 0,
        paying: !1,
        isAndroid:false,
    },
    onLoad: function(a) {
        var e = this;
        this.setData({
            system: t.globalData.system
        }), t.util.checkLogin().then(function() {
            e.getWxappInfo(), e.getBalance(), e.getTasks(), e.getGoodsList(), e.setData({
                pageIsLoad: !0
            });
        });
        const systemInfo = wx.getSystemInfoSync()
        const isIOS = systemInfo.platform === 'ios'
        if (isIOS) {
          this.setData({
            isAndroid:false
          })
        } else {
            this.setData({
                isAndroid:true
            })
        }

    },
    onShow: function() {
        this.data.pageIsLoad && this.getBalance();
    },
    getGoodsList: function() {
        var a = this;
        t.util.request({
            url: "/wxapp/getGoodsList"
        }).then(function(t) {
            var e = t.data;
            e.forEach(function(t) {
                t.is_default && a.setData({
                    goods_id: t.id
                });
            }), a.setData({
                goodsList: e
            });
        });
    },
    getTasks: function() {
        var e = this;
        t.util.request({
            url: "/wxapp/getTasks",
            loading: !1
        }).then(function(t) {
            e.setData({
                tasks: t.data
            }), t.data && t.data.ad && !a && e.initRewardAd();
        });
    },
    getBalance: function() {
        var a = this;
        t.util.request({
            url: "/wxapp/getBalance",
            loading: !1
        }).then(function(t) {
            a.setData({
                balance: t.data.balance,
                vip_expire_time: t.data.vip_expire_time
            });
        });
    },
    chooseGoods: function(t) {
        var a = t.currentTarget.dataset.id;
        this.setData({
            goods_id: a
        });
    },
    getWxappInfo: function() {
        var a = this;
        t.util.request({
            url: "/wxapp/getWxappInfo",
            loading: !1
        }).then(function(t) {
            a.setData({
                is_check: t.data.is_check,
                is_ios_pay: t.data.is_ios_pay
            });
        });
    },
    doPayGoods: function() {
        var a = this;
        if (this.data.paying) return !1;
        var e = this.data.goods_id;
        if (0 == e) return t.util.message("请选择充值套餐"), !1;
        this.setData({
            paying: !0
        }), setTimeout(function() {
            a.setData({
                paying: !1
            }, 2e3);
        }), t.util.request({
            url: "/wxapp/createOrder",
            data: {
                goods_id: e
            }
        }).then(function(a) {
            wx.requestPayment({
                timeStamp: a.data.timeStamp,
                nonceStr: a.data.nonceStr,
                package: a.data.package,
                signType: "MD5",
                paySign: a.data.paySign,
                success: function(a) {
                    t.util.message("支付成功", "error", function() {
                        wx.redirectTo({
                            url: "/pages/index/index"
                        });
                    });
                },
                fail: function(a) {
                    t.util.message("支付失败，请重试", "error");
                }
            });
        });
    },
    toShare: function() {
        t.util.message("请回到首页，点右上角“...”，转发给朋友", "error", function() {
            wx.switchTab({
                url: "/pages/index/index"
            });
        });
    },
    initRewardAd: function() {
        var e = this;
        a = null;
        var i = this.data.tasks.ad.ad_unit_id;
        wx.createRewardedVideoAd && !a && ((a = wx.createRewardedVideoAd({
            adUnitId: i
        })).onLoad(function() {
            console.log("reward ad onload");
        }), a.onError(function(t) {
            console.log("reward ad error"), e.initRewardAd();
        }), a.onClose(function(a) {
            console.log("reward ad onclose", a), a && a.isEnded && t.util.request({
                url: "/wxapp/doAd",
                data: {
                    ad_unit_id: i
                },
                loading: !1
            }).then(function(a) {
                t.util.message(a.message), e.getTasks(), e.getBalance();
            });
        }));
    },
    showTaskAd: function() {
        if (wx.showLoading({
            title: "加载中"
        }), a || this.initRewardAd(), !a) return wx.hideLoading(), void t.util.message("广告加载失败，请重试", "error");
        a.load().then(function() {
            a.show(), setTimeout(function() {
                wx.hideLoading();
            }, 1500);
        }).catch(function() {
            wx.hideLoading(), t.util.message("广告加载失败，请重试", "error");
        });
    }
});